package org.big.usermanager;


import jakarta.validation.Valid;
import org.big.usermanager.dto.*;
import org.big.usermanager.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    /**
     * 创建用户
     */
    @PostMapping
//    @ApiOperation(value = "创建用户账户", notes = "创建新的用户账户，支持多种注册方式")
    public ResponseEntity<ApiResponse<UserResponse>> createUser(
            @Valid @RequestBody CreateUserRequest request) {
        log.info("Creating user: {}", request);

        UserResponse user = userService.createUser(request);

        return ResponseEntity.status(HttpStatus.CREATED)
                .body(ApiResponse.success("用户创建成功", user));
    }

    /**
     * 获取用户详情
     */
    @GetMapping("/{userId}")
//    @ApiOperation(value = "获取用户详情", notes = "根据用户ID获取用户详细信息")
    public ResponseEntity<ApiResponse<UserResponse>> getUserById(
//            @ApiParam(value = "用户ID", required = true, example = "100001")
            @PathVariable Long userId) {
        log.info("getUserById");
        UserResponse user = userService.getUserById(userId);

        return ResponseEntity.ok(ApiResponse.success("获取用户信息成功", user));
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/{userId}")
//    @ApiOperation(value = "更新用户信息", notes = "更新用户基本信息，支持部分更新")
    public ResponseEntity<ApiResponse<UserResponse>> updateUser(
//            @ApiParam(value = "用户ID", required = true, example = "100001")
            @PathVariable Long userId,
            @Valid @RequestBody UpdateUserRequest request) {
        UserResponse user = userService.updateUser(userId, request);
        return ResponseEntity.ok(ApiResponse.success("用户信息更新成功", user));
    }

    /**
     * 删除用户账户
     */
    @DeleteMapping("/{userId}")
//    @ApiOperation(value = "删除用户账户", notes = "软删除用户账户，将状态标记为已删除")
    public ResponseEntity<ApiResponse<Void>> deleteUser(
//            @ApiParam(value = "用户ID", required = true, example = "100001")
            @PathVariable Long userId) {
        userService.deleteUser(userId);
        return ResponseEntity.ok(ApiResponse.success("用户删除成功", null));
    }

    /**
     * 更新用户状态
     */
    @PatchMapping("/{userId}/status")
//    @ApiOperation(value = "更新用户状态", notes = "更新用户账户状态，支持启用、禁用、注销等状态变更")
    public ResponseEntity<ApiResponse<Void>> updateUserStatus(
//            @ApiParam(value = "用户ID", required = true, example = "100001")
            @PathVariable Long userId,
            @Valid @RequestBody UpdateStatusRequest request) {
        userService.updateUserStatus(userId, request);
        return ResponseEntity.ok(ApiResponse.success("用户状态更新成功", null));
    }

    /**
     * 修改密码
     */
    @PutMapping("/{userId}/password")
//    @ApiOperation(value = "修改密码", notes = "用户修改登录密码")
    public ResponseEntity<ApiResponse<Void>> changePassword(
//            @ApiParam(value = "用户ID", required = true, example = "100001")
            @PathVariable Long userId,
            @Valid @RequestBody ChangePasswordRequest request) {
        userService.changePassword(userId, request);
        return ResponseEntity.ok(ApiResponse.success("密码修改成功", null));
    }

    /**
     * 根据手机号查询用户
     */
    @GetMapping("/by-phone/{phone}")
//    @ApiOperation(value = "根据手机号查询用户", notes = "根据手机号查询用户信息")
    public ResponseEntity<ApiResponse<UserResponse>> getUserByPhone(
//            @ApiParam(value = "手机号码", required = true, example = "13800000001")
            @PathVariable String phone) {
        UserResponse user = userService.getUserByPhone(phone);
        return ResponseEntity.ok(ApiResponse.success("获取用户信息成功", user));
    }

    /**
     * 根据邮箱查询用户
     */
    @GetMapping("/by-email/{email}")
//    @ApiOperation(value = "根据邮箱查询用户", notes = "根据邮箱查询用户信息")
    public ResponseEntity<ApiResponse<UserResponse>> getUserByEmail(
//            @ApiParam(value = "邮箱地址", required = true, example = "user@example.com")
            @PathVariable String email) {
        UserResponse user = userService.getUserByEmail(email);
        return ResponseEntity.ok(ApiResponse.success("获取用户信息成功", user));
    }

    /**
     * 批量获取用户信息
     */
    @PostMapping("/batch")
//    @ApiOperation(value = "批量获取用户信息", notes = "根据用户ID列表批量获取用户基本信息")
    public ResponseEntity<ApiResponse<List<UserResponse>>> getUsersBatch(
            @Valid @RequestBody BatchUserRequest request) {
        List<UserResponse> users = userService.getUsersBatch(request.getUserIds());
        return ResponseEntity.ok(ApiResponse.success("批量获取用户信息成功", users));
    }
}